package sample;

import javax.swing.text.html.HTML;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame{
    public Calculator()  {
        //3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));

        //1个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


}
}
class MyCalculatorListener implements ActionListener
{
    //获取计算器这个对象，在一个类中组合另外的类；
   // Calculator calculator=null;
    //获取三个变量
    private TextField num1,num2,num3;
    public MyCalculatorListener(TextField num1,TextField num2,TextField num3)
    {
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }


    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        //1.获得加数和被加数
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //2.将这个值+法运算后，放到第三个框
        num3.setText(""+(n1+n2));
        //清除前两个框
        //num1.setText("");
        //num2.setText("");


    }
}